package com.xuebank.nfe.nfecar.activity

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import android.widget.Toast
import com.alibaba.android.vlayout.VirtualLayoutManager
import com.alibaba.android.vlayout.layout.LinearLayoutHelper
import com.licrafter.tagview.DIRECTION
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.xuebank.nfe.nfecar.R
import com.xuebank.nfe.nfecar.adapter.CommonItemDecoration
import com.xuebank.nfe.nfecar.adapter.ListShopAdapter
import com.xuebank.nfe.nfecar.adapter.OnItemClickListener
import com.xuebank.nfe.nfecar.beas.BaseActivity
import com.xuebank.nfe.nfecar.isOk
import com.xuebank.nfe.nfecar.model.ListShop
import com.xuebank.nfe.nfecar.model.ShopListParameterModel
import com.xuebank.nfe.nfecar.model.TagGroupModel
import com.xuebank.nfe.nfecar.utils.DirectionUtils
import com.xuebank.nfe.nfecar.utils.XCRecyclerView
import com.xuebank.nfe.nfecar.utils.network.ServiceGenerator
import com.xuebank.nfe.nfecar.utils.network.service.CommonService
import com.xuebank.nfe.nfecar.view.FullyLinearLayoutManager
import rx.android.schedulers.AndroidSchedulers
import rx.schedulers.Schedulers
import java.util.zip.Inflater

/**
 * 标签页面
 */

class PreviewActivity : BaseActivity(),View.OnClickListener,OnItemClickListener{


    private var mRecyclerView: XCRecyclerView? = null
    private var mCurPage = 1
    private var mAdapter: ListShopAdapter? = null
    private var dian:View? = null
    var mLayoutManager = FullyLinearLayoutManager(this)
    var isPopupWindowShow = 1
    var accountFlowViewType :String?= null
    var isClick:String? = null
    public var list: ArrayList<ListShop>? = arrayListOf()
    private var mNot: LinearLayout? = null
    private var mScroll: SmartRefreshLayout? = null
    private var mShopListParameterModel : ShopListParameterModel ? = null
    var tagsss:TagGroupModel.Tag ? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_preview)
    }
    companion object {
        fun setUp(context:Context) {
            val intent  = Intent(context,PreviewActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun onViewClick(v: View) {
        super.onViewClick(v)
    }


    override fun cleanData() {
        super.cleanData()
        mScroll?.finishRefresh()
    }


    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        mShopListParameterModel = ShopListParameterModel(mCurPage,10)
        requestShop()
    }

    override fun findViews() {
        super.findViews()
        mRecyclerView = findViewById(R.id.act_user_transaction_history_listview_new)
        mScroll = findViewById(R.id.scroll)

    }

    override fun initViews() {
        super.initViews()







        mScroll?.setOnRefreshListener { refreshLayout ->
            mCurPage = 1
            list?.clear()
            requestShop()
        }

        mScroll?.setOnLoadMoreListener { refreshLayout ->
            mCurPage += 1
            requestShop()
        }
    }


    fun requestShop() {
        if (mShopListParameterModel != null) {
            ServiceGenerator.createService(CommonService::class.java)
                    .shopList(mShopListParameterModel!!)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .compose(bindToLifecycle())
                    .subscribe({
                        if (it.isOk()) {
                            if (mCurPage == 1 && (it.result?.list?.size == 0 || null == it.result?.list)) {
                                mScroll?.visibility = View.GONE
                                Toast.makeText(this,"商家类表为空",Toast.LENGTH_SHORT).show()
                            } else {
                                mScroll?.visibility = View.VISIBLE
                                list = it.result?.list
                                val layoutmanager = VirtualLayoutManager(this)
                                mRecyclerView?.layoutManager = layoutmanager
                                //注释的分割线
//                                mRecyclerView?.addItemDecoration(CommonItemDecoration(Color.WHITE))
                                mAdapter = ListShopAdapter(list!!, LinearLayoutHelper(), this)
                                mRecyclerView?.adapter = mAdapter
                                list?.addAll(it.result?.list!!)

                                if (it.result?.lastPage?.compareTo(mCurPage) == 0) {
                                    mScroll?.finishLoadMoreWithNoMoreData()
                                    mScroll?.setNoMoreData(false)
                                    val view = LayoutInflater.from(mRecyclerView?.context).inflate(R.layout.preview_foot,mRecyclerView,false)
                                    mRecyclerView?.addFooterView(view)
                                }


                                mAdapter?.notifyDataSetChanged()
                            }

                        }
                    })
        }
    }
    override fun onItemClick(view: View, position: Int) {


        var intent = Intent()
        intent.putExtra("aoiaoidj",createTagGroup(list?.get(position)?.content?:"",list?.get(position)?.id?:0))
//        intent.putExtra("sbiwu",tagsss)
        setResult(Activity.RESULT_OK,intent)
        this.finish()
    }

    private fun setTagDirection(tagList: TagGroupModel.Tag) {

        tagList.direction = DirectionUtils.getValue(DIRECTION.RIGHT_CENTER)

    }

    fun createTagGroup(tags:String,tagId:Int): TagGroupModel {
        var tagsss:TagGroupModel.Tag ? = null
        val model = TagGroupModel()
        val tagList = java.util.ArrayList<TagGroupModel.Tag>()
//        val str01 = tag01.getText().toString()

        //        String fangxiang = DIRECTION.LEFT_CENTER;
        //        String str02 = tag02.getText().toString();
        //        String str03 = tag03.getText().toString();
        val tag = TagGroupModel.Tag()
        if (!TextUtils.isEmpty(tags)) {
            tag.name = tags
            tag.direction = 0
            tag.id = tagId
        }



        tagsss = tag
        model.tags?.add(tag)
        setTagDirection(tag)
        model.percentX = 0.5f
        model.percentY = 0.5f
        return model
    }
}
